package com.moniteach.server.servlet;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.moniteach.server.db.bo.Group;
import com.moniteach.server.db.bo.Predmet;
import com.moniteach.server.db.bo.Table;
import com.moniteach.server.db.connector.PMF;
import com.moniteach.utils.Resource;

public class TableServlet extends CommonServlet {
	private String action;
	private String id;
	private String group;
	private String predmet;

	@Override
	protected void prepare(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.setCharacterEncoding("UTF-8");

		action = req.getParameter(Resource.Const.action);
		if ("".equals(action) || action == null) {
			resp.getWriter().print(Resource.Const.no + "ACTION");
			return;
		}

		id = req.getParameter(Resource.Const.id);
		group = req.getParameter(Resource.Const.group);
		predmet = req.getParameter(Resource.Const.predmet);

		String response = "";
		if (Resource.Action.CREATE.equals(action)) {
			response = create();
		} else if (Resource.Action.GET_BY_ID.equals(action)) {
			response = getById();
		} else if (Resource.Action.MODIFY.equals(action)) {
			response = modify();
		}
		resp.getWriter().print(response);

	}

	@Override
	protected String create() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Long groupLong = Long.valueOf(group);
			Group entityGroup = pm.getObjectById(Group.class, groupLong);

			Long predmetLong = Long.valueOf(predmet);
			Predmet entityPredmet = pm
					.getObjectById(Predmet.class, predmetLong);
			Table oneTable = new Table(entityGroup.getId(),
					entityPredmet.getId());
			pm.makePersistent(oneTable);
			return String.valueOf(oneTable.getId());
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}
	}

	@Override
	protected String get() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getById() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Long idLong = Long.valueOf(id);
			Group entityGroup = pm.getObjectById(Group.class, idLong);
			return entityGroup.toJSONObject().toString();
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}
	}

	@Override
	protected String getAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String modify() {
		// PersistenceManager pm = PMF.get().getPersistenceManager();
		// try {
		// Long idLong = Long.valueOf(id);
		// Group entityGroup = pm.getObjectById(Group.class, idLong);
		//
		// if (name != null) {
		// entityGroup.setName(name);
		// }
		// if (description != null) {
		// entityGroup.setDescription(description);
		// }
		// if (starosta != null) {
		// Long starostaLong = Long.valueOf(starosta);
		// UserInfo entityUser = pm.getObjectById(UserInfo.class,
		// starostaLong);
		// entityGroup.setStarosta(entityUser.getId());
		// }
		// pm.makePersistent(entityGroup);
		// return C.ok);
		// } catch (Exception e) {
		// return C.no);
		// } finally {
		// pm.close();
		// }
		return null;
	}
}
